package com.artisan.provider.controller;

import com.artisan.provider.contanent.Contanents;
import com.artisan.provider.covtransformation.CovDiscountTransformation;
import com.artisan.provider.domain.ZgDiscountCode;
import com.artisan.provider.dto.ZgDiscountCodeDto;
import com.artisan.provider.service.ZgDiscountCodeService;
import com.artisan.provider.utils.Request;
import com.artisan.provider.vo.ZgDiscountCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ZgDiscountCodeController {

    @Autowired
    ZgDiscountCodeService zgDiscountCodeService;



    /***
     * 查询所有优惠券信息
     * @param zgDiscountCodeDto
     * @param page
     * @param limit
     * @return
     */
    @PostMapping(value = "/api/discountCode/find/all/{page}/{limit}")
    public Request zgDiscountCodeFindAll(@RequestBody(required = false) ZgDiscountCodeDto zgDiscountCodeDto,
                                        @PathVariable(value = "page",required = false) int page,
                                        @PathVariable(value = "limit",required = false)int limit
                                        ){

        List<ZgDiscountCode> zgDiscountCodes = zgDiscountCodeService.findAll(zgDiscountCodeDto,page,limit);
        List<ZgDiscountCodeVo> zgDiscountCodeVos = CovDiscountTransformation.INSTANCE.zgDiscountCodeVos(zgDiscountCodes);
        if (zgDiscountCodes != null){
            return new Request(Contanents.DATA_SUCCESS,true,zgDiscountCodeVos);
        }else{
            return new Request(Contanents.DATA_FAILED,false,new ArrayList<>());
        }

    }

    /**
     * 数据的添加业务
     * @param zgDiscountCodeDto
     * @return
     */
    @PostMapping(value = "/api/discountCode/add")
    public Request zgDiscountCodeAdd(@RequestBody ZgDiscountCodeDto zgDiscountCodeDto){
        ZgDiscountCode zgDiscountCode = CovDiscountTransformation.INSTANCE.zgDiscountCode(zgDiscountCodeDto);
        if(zgDiscountCodeService.zgDiscountCodeAdd(zgDiscountCode) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的修改业务
     * @param zgDiscountCodeDto
     * @return
     */
    @PutMapping(value = "/api/discountCode/update")
    public Request zgDiscountCodeUpdate(@RequestBody ZgDiscountCodeDto zgDiscountCodeDto){
        ZgDiscountCode zgDiscountCode = CovDiscountTransformation.INSTANCE.zgDiscountCode(zgDiscountCodeDto);
        if(zgDiscountCodeService.zgDiscountCodeUpdate(zgDiscountCode) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的删除业务
     * @param id
     * @return
     */
    @DeleteMapping(value = "/api/discountCode/delete/{id}")
    public Request zgDiscountCodeDelete(@PathVariable(value = "id")int id){

        if(zgDiscountCodeService.zgDiscountCodeDelete(id) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

}
